package com.weiyi.chat.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hyphenate.chat.EMClient;
import com.weiyi.chat.R;
import com.weiyi.chat.common.Constant;
import com.weiyi.chat.common.base.BaseActivity;
import com.weiyi.chat.common.base.BaseFragment;
import com.weiyi.chat.transformer.GlideCircleTransformer;
import com.weiyi.chat.ui.activity.ResetPswActivity;
import com.weiyi.chat.ui.activity.SendNoticeActivity;
import com.weiyi.chat.ui.activity.SettingActivity;
import com.weiyi.chat.ui.activity.UserProfileActivity;
import com.weiyi.chat.utils.PreferenceUtil;
import com.weiyi.chat.view.widget.HeadLayout;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Lee on 2017/8/22 0022.
 */
public class MeFragment extends BaseFragment {
    @BindView(R.id.hl_head)
    HeadLayout mHlHeadLayout;
    @BindView(R.id.iv_avatar)
    ImageView mIvAvatar;
    @BindView(R.id.tv_personal_name)
    TextView mTvPersonalName;
    @BindView(R.id.tv_personal_info)
    TextView mTvPersonalInfo;
    @BindView(R.id.ll_personal_info)
    LinearLayout mLlPersonalInfo;
    @BindView(R.id.tv_reset_psw)
    TextView mTvResetPsw;
    @BindView(R.id.tv_setting)
    TextView mTvSetting;
    Unbinder unbinder;
    @BindView(R.id.tv_send_notice)
    TextView mTvSendNotice;
    Unbinder unbinder1;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_main_me;
    }

    @Override
    public void initViews() {
        mHlHeadLayout.setBackVisibility(false);
        Glide.with(mContext)
                .load("http://bimg.tubaba.com.cn/http://img3.imgtn.bdimg.com/it/u=1961347126,526248305&fm=23&gp=0.jpg")
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .transform(new GlideCircleTransformer(mContext))
                .into(mIvAvatar);
        try {
            if (PreferenceUtil.getValueB(Constant.SP_ISLOGIN, false, mContext)){

                String bus_owner = PreferenceUtil.getValue(Constant.SP_BUS_OWNER, mContext);
                String bus_mobile = PreferenceUtil.getValue(Constant.SP_BUS_MOBILE, mContext);

                mTvPersonalName.setText(bus_owner);
                mTvPersonalInfo.setText("手机号：" + bus_mobile);
            }else {
                ((BaseActivity) mContext).toLogin();
            }
        }catch (Exception e) {
            ((BaseActivity) mContext).toLogin();
        }
    }

    @Override
    public void initLoadData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        String bus_owner = PreferenceUtil.getValue(Constant.SP_BUS_OWNER, mContext);
        mTvPersonalName.setText(bus_owner);
        Glide.with(mContext).load(PreferenceUtil.getValue(Constant.SP_USER_AVATAR, mContext)).into(mIvAvatar);

    }

    @OnClick({R.id.tv_reset_psw, R.id.tv_setting, R.id.ll_personal_info, R.id.tv_send_notice})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_reset_psw:
                startActivity(new Intent(mContext, ResetPswActivity.class));
                break;
            case R.id.tv_setting:
                startActivity(new Intent(mContext, SettingActivity.class));
                break;
            case R.id.ll_personal_info:
                startActivity(new Intent(mContext, UserProfileActivity.class)
                        .putExtra("setting", true)
                        .putExtra("username", EMClient.getInstance().getCurrentUser())
                        .putExtra("uid", PreferenceUtil.getValue(Constant.SP_BUS_ID, mContext))
                        .putExtra("usernick", PreferenceUtil.getValue(Constant.SP_BUS_OWNER, mContext)));
                break;
            case R.id.tv_send_notice:
                startActivity(new Intent(mContext, SendNoticeActivity.class));
                break;
        }
    }

}
